<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*,
com.brother.model.*,com.ibatis.common.util.*,org.apache.commons.lang.*,com.brother.service.*,
com.brother.realtime.config.*,com.brother.realtime.cache.*,com.brother.realtime.*,java.io.*,
org.apache.commons.fileupload.*,org.apache.commons.fileupload.disk.DiskFileItemFactory,
org.apache.commons.fileupload.servlet.ServletFileUpload,com.brother.util.*,java.nio.ByteBuffer"   pageEncoding="UTF-8"%><%

String trackerName = request.getParameter("trackerName");
TrackerService tks = new TrackerService();
Tracker tracker = null;

try {
	   File tempfile = new File(System.getProperty("java.io.tmpdir"));//采用系统临时文件目录      
	   DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
	   diskFileItemFactory.setSizeThreshold(4096);  // 设置缓冲区大小，这里是4kb
	   diskFileItemFactory.setRepository(tempfile);  // 设置缓冲区目录
	   ServletFileUpload fu = new ServletFileUpload(diskFileItemFactory);
	   fu.setSizeMax(1024*300);
	   List fileItems = fu.parseRequest(request);
	   Iterator i = fileItems.iterator();
	   //String uploadPath = CommonProperties.getValue("firmware.file.upload.path"); // 上传文件的目录
	  // File f = new File(uploadPath);
	   //if(!f.exists()){
		//   f.mkdirs();
	  // }
	   ByteBuffer bf = null;
	   while (i.hasNext()) {
		    FileItem fi = (FileItem) i.next();
		    if(fi.isFormField()){
		    	String fname = fi.getFieldName();
		    	if(fname.equalsIgnoreCase("trackerName")){
		    		tracker = tks.getTrackerByFieldName("trackerName", fi.getString("UTF-8"));
		    		trackerName = fi.getString("UTF-8");
		    	}
		    }
		    String fileName = fi.getName();
		    if (fileName != null) {
		     	//File fullFile = new File(fi.getName());
		     	//File savedFile = new File(uploadPath, UUID.randomUUID().toString().replaceAll("-","")+fullFile.getName());
		     	bf = ByteBuffer.allocate((int)fi.getSize());
		     	InputStream in = fi.getInputStream();
		     	byte[] rb = new byte[1024];
		     	int read = -1;
		     	while((read=in.read(rb,0,1024)) != -1){
		     		bf.put(rb, 0, read);
		     	}
		     	in.close();
		     	fi.delete();
		    }
	   }
	   System.out.println("upload succeed for tracker "+trackerName+", file size="+bf.capacity());
	   UpgradeFirmware  firmware = new UpgradeFirmware(bf);
	   
	   ActionData adata = new ActionData();
	   adata.setTrackerId(tracker.getTrackerid());
	   adata.setAction("transferFirmare");

	   ParametersSetCache.getInstance().addAction(adata);
	   
		if(ProcessorPool.getInstance().getProcessorByTrackerId(tracker.getTrackerid()) != null){
			ProcessorPool.getInstance().getProcessorByTrackerId(tracker.getTrackerid()).addConfig(firmware);
		}
	   
	   out.print(tracker.getTrackerid());
	  } catch (Exception e) {
	   		e.printStackTrace();
	  }finally{
		  com.brother.database.DataBase.closeThreadConnection();
	  }
	 
%>